package com.xkcoding.helloworld.service.proxy;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;

/**
 * TODO
 *
 * @Author ziyiHuang
 * @Date 2022/12/1 23:09
 **/
public class JdkDemoServiceProxy implements DemoServiceProxy, InvocationHandler {

    /**
     * 目标对象
     */
    private Object obj;

    @Override
    public <T> T getProxyInstance(T target) {
        obj = target;
        T instance = (T) Proxy.newProxyInstance(target.getClass().getClassLoader(), target.getClass().getInterfaces(), this);
        return instance;
    }

    @Override
    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
        System.out.println("jdk 代理实现... ");
        return method.invoke(obj,args);
    }


}
